package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.book.service.BookService;
import com.itany.constant.ResponseConstant;
import com.itany.constant.StatusConstant;
import com.itany.exception.FileUploadErrorException;
import com.itany.exception.FlagDisEnableException;
import com.itany.exception.FlageEnableException;
import com.itany.exception.RequestParamException;
import com.itany.pojo.*;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Auther:ws@itany.com
 * Date:20-10-10 下午3:35
 * Version:1.0
 * Description:
 */
@Controller
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;


    /**
     * 查询书籍
     * @param page
     * @param rowsbook
     * @param
     * @return
     */
    @RequestMapping("/findBookAll")
    @ResponseBody
    public Map<String,Object> findBooks(@RequestParam(defaultValue="1")Integer page, @RequestParam(defaultValue="10")Integer rows,
                                        String bookName, String ISBN, Integer pressId, Integer supplierId){
        PageInfo<Book> bookPages = bookService.findByParam(page, rows, bookName,ISBN,pressId,supplierId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",bookPages.getTotal());
        map.put("rows",bookPages.getList());
        return map;
    }


    @ResponseBody
    @RequestMapping("/findSupplier")
    public ActionResult findAllSupplier(){
        ActionResult result=new ActionResult();
        try {
            List<Supplier> suppliers = bookService.findSupplier();
            result.setData(suppliers);
            result.setStatus(ResponseConstant.RESPONSE_CODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.setStatus(ResponseConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }


    @RequestMapping("/addBook")
    @ResponseBody
    public ActionResult addBook(String bookname, String ISBN, String price, String number, String groupprice,
                                String layout, String register, String weight, String bookintroduce, String authorintroduce,
                                String author, String publishdate, String fromat, String pagenumber, String press,
                                MultipartFile file, HttpSession session){
        ActionResult result = new ActionResult();

        try {
            bookService.addBook(bookname,ISBN,price,number,groupprice,
                    layout,register,weight,bookintroduce,authorintroduce,
                    author,publishdate,fromat,pagenumber,press,
                    file,1);
            result.setStatus(ResponseConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParamException e) {
            e.printStackTrace();
            result.setStatus(ResponseConstant.RESPONSE_CODE_FAIL);
        } catch (FileUploadErrorException e) {
            e.printStackTrace();
            result.setStatus(ResponseConstant.RESPONSE_CODE_FAIL);
        }
        result.setStatus(ResponseConstant.RESPONSE_CODE_SUCCESS);
        return result;
    }

    @ResponseBody
    @RequestMapping("/findPress")
    public ActionResult findPress(){
        ActionResult ar=new ActionResult();
        List<Press> presses= bookService.findPress();
        ar.setStatus(ResponseConstant.RESPONSE_CODE_SUCCESS);
        ar.setMsg("查询成功");
        ar.setData(presses);
        return ar;

    }


    @ResponseBody
    @RequestMapping("/DisEnableFlag")
    public ActionResult DisEnableFlag(String isbn){
        ActionResult result=new ActionResult();

        try {
            bookService.DisEnableFlag(isbn);
            result.setStatus(ResponseConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParamException e) {
            e.printStackTrace();
            result.setStatus(ResponseConstant.RESPONSE_CODE_FAIL);
        } catch (FlagDisEnableException e) {
            e.printStackTrace();
            result.setStatus(ResponseConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }


    @ResponseBody
    @RequestMapping("/EnableFlag")
    public ActionResult EnableFlag(String isbn){
        ActionResult result=new ActionResult();
        try {
            bookService.EnableFlag(isbn);
            result.setStatus(ResponseConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParamException e) {
            e.printStackTrace();
            result.setStatus(ResponseConstant.RESPONSE_CODE_FAIL);
        } catch (FlageEnableException e) {
            e.printStackTrace();
            result.setStatus(ResponseConstant.RESPONSE_CODE_FAIL);
        }
        return result;
    }

    @RequestMapping("/examineSupplier")
    @ResponseBody
    public Map<String,Object> examineSupplier(@RequestParam(defaultValue="1")Integer page,
                                              @RequestParam(defaultValue="10")Integer rows,String bookname,String isbn,HttpSession session){
        Map<String,Object> map=new HashMap<>();
        ManagerUser user = (ManagerUser) session.getAttribute("User");
        PageInfo<Examine> info=bookService.examineSupplier(page,rows,bookname,isbn,user.getSupplierid());
//        System.out.println(info.getList());
        map.put("total",info.getTotal());
        map.put("rows",info.getList());
        return map;
    }
}
